<?php
	require_once(dirname(__FILE__).'/easy_runner.php');
	require_once(dirname(__FILE__).'/../router.php');
	
	class WhenRouterIsExtractingParameters extends UnitTestCase {
	
		public function testRouterExtractsParametersFromMatchingPattern() {
			$router = new Router();
			$pattern = new RoutePattern('articles/:id');
			$pattern->setRegexRuleFor('id', '/^[0-9]$/');
			$router->addPattern($pattern);
			$router->addPattern(new RoutePattern('articles/add'));
			$parameters = $router->extractParameters('articles/2');
			$this->assertEqual($parameters['id'], 2);
		}

		public function testRouterReturnsFalseWhenNoMatchFound() {
			$router = new Router();
			$pattern = new RoutePattern('articles/:id');
			$pattern->setRegexRuleFor('id', '/^[0-9]$/');
			$router->addPattern($pattern);
			$router->addPattern(new RoutePattern('articles/add'));
			$parameters = $router->extractParameters('articles/pes-mi-sezral-domaci-ukol');
			$this->assertFalse($parameters);
		}
		
		public function testRouterMatchesPatternWithHigherPriority() {   
			$router = new Router();
			$router->addPattern(new RoutePattern('articles/:sid'));
			$router->addPattern(new RoutePattern('articles/add'));
			$parameters = $router->extractParameters('articles/add');

			// array() proto, ze uri nenese parametry
			$this->assertEqual($parameters, array());
		}
		
		public function testRouterReturnsDefaultParametersOnNoMatch() {
			$router = new Router();
			$router->setDefaultParameters(array('action' => 'default'));
			$parameters = $router->extractParameters('bad');
			$this->assertEqual($parameters, array('action' => 'default'));
		}
	}
	
	class WhenRouterIsCreatingPath extends UnitTestCase {
		
		public function testRouterCanCreateSimplePath() {
			$router = new Router();
			$router->addPattern(new RoutePattern('article/:sid'));
			$router->addPattern(new RoutePattern('dogs/:race'));
			$path = $router->createPath(array("sid"=>"how-to-feed-your-dog"));
			$this->assertEqual($path, "article/how-to-feed-your-dog");
			$path = $router->createPath(array("race"=>"mixed"));
			$this->assertEqual($path, "dogs/mixed");
		}

		public function testRouterUsesPriorities() {
			$router = new Router();
			$router->addPattern(new RoutePattern('articles/:sid'));
			$router->addPattern(new RoutePattern(':sid/add'));			
			$path = $router->createPath(array("sid"=>"how-to-build-a-house"));
			$this->assertEqual($path, "articles/how-to-build-a-house");
		}
	}
	
	if(EasyRunner::isRunner()) {
		$test = new GroupTest('RouterTests');
		$test->addTestCase(new WhenRouterIsExtractingParameters());
		$test->addTestCase(new WhenRouterIsCreatingPath());		
		EasyRunner::run($test);
	}
?>